{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import easytrader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda3\\lib\\site-packages\\pywinauto\\application.py:1064: UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)\n",
      "  UserWarning)\n"
     ]
    }
   ],
   "source": [
    "user = easytrader.use('ht_client')\n",
    "user.prepare(user='666626218349', password='836198', comm_password='sam155')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "balance = user.balance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "880.74"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balance['资金余额']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "股票余额： 30000\n"
     ]
    }
   ],
   "source": [
    "#仓位信息\n",
    "position = user.position\n",
    "for stock in position:\n",
    "    if stock['证券代码'] == '512660':\n",
    "        print(\"股票余额： %s\"%stock['股票余额'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "股票余额： 30000\n",
      "<class 'int'>\n",
      "股票余额大于0\n"
     ]
    }
   ],
   "source": [
    "for stock in position:\n",
    "    if stock['证券代码'] == '512660':\n",
    "        print(\"股票余额： %s\"%stock['股票余额'])\n",
    "        print(type(stock['股票余额']))\n",
    "        if stock['股票余额'] > 0:\n",
    "            print(\"股票余额大于0\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "TradeError",
     "evalue": "提交失败：[120147][当前时间不允许委托] [curr_time=155951] 。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTradeError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-219fc7398a8d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m#return {'entrust_no': 'xxxxxxxx'}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m#卖出\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0muser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msell\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'512660'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprice\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1.00\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mamount\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[1;31m#return {'entrust_no': 'xxxxxxxx'}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;31m#撤单\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\easytrader\\clienttrader.py\u001b[0m in \u001b[0;36msell\u001b[1;34m(self, security, price, amount, **kwargs)\u001b[0m\n\u001b[0;32m    150\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_switch_left_menus\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"卖出[F2]\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    151\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 152\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrade\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msecurity\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprice\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mamount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    153\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    154\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mmarket_buy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msecurity\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mamount\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mttype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\easytrader\\clienttrader.py\u001b[0m in \u001b[0;36mtrade\u001b[1;34m(self, security, price, amount)\u001b[0m\n\u001b[0;32m    284\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    285\u001b[0m         return self._handle_pop_dialogs(\n\u001b[1;32m--> 286\u001b[1;33m             \u001b[0mhandler_class\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpop_dialog_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTradePopDialogHandler\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    287\u001b[0m         )\n\u001b[0;32m    288\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\easytrader\\clienttrader.py\u001b[0m in \u001b[0;36m_handle_pop_dialogs\u001b[1;34m(self, handler_class)\u001b[0m\n\u001b[0;32m    382\u001b[0m             \u001b[0mtitle\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_pop_dialog_title\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    383\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 384\u001b[1;33m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhandler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtitle\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    385\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    386\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\easytrader\\pop_dialog_handler.py\u001b[0m in \u001b[0;36mhandle\u001b[1;34m(self, title)\u001b[0m\n\u001b[0;32m     68\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_submit_by_click\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     69\u001b[0m             \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.05\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 70\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mexceptions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTradeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     71\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_close\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     72\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTradeError\u001b[0m: 提交失败：[120147][当前时间不允许委托] [curr_time=155951] 。"
     ]
    }
   ],
   "source": [
    "#买入\n",
    "#user.buy('162411', price=0.55, amount=100)\n",
    "#return {'entrust_no': 'xxxxxxxx'}\n",
    "#卖出\n",
    "user.sell('512660', price=1.00, amount=100)\n",
    "#return {'entrust_no': 'xxxxxxxx'}\n",
    "#撤单\n",
    "#user.cancel_entrust('buy/sell 获取的 entrust_no')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'成交日期': 20190311,\n",
       "  '成交时间': '15:01:54',\n",
       "  '证券代码': '131810',\n",
       "  '证券名称': 'Ｒ-001',\n",
       "  '操作': '卖出',\n",
       "  '成交数量': 370,\n",
       "  '成交均价': 1.85,\n",
       "  '成交金额': 37000.0,\n",
       "  '交易市场': '深圳Ａ',\n",
       "  '股东帐户': 206244018,\n",
       "  '成交编号': 16110,\n",
       "  '合同编号': '16488',\n",
       "  'Unnamed: 12': ''},\n",
       " {'成交日期': 20190311,\n",
       "  '成交时间': '14:42:10',\n",
       "  '证券代码': '002131',\n",
       "  '证券名称': '利欧股份',\n",
       "  '操作': '卖出',\n",
       "  '成交数量': 5813,\n",
       "  '成交均价': 2.3,\n",
       "  '成交金额': 13369.9,\n",
       "  '交易市场': '深圳Ａ',\n",
       "  '股东帐户': 206244018,\n",
       "  '成交编号': 14978,\n",
       "  '合同编号': '15398',\n",
       "  'Unnamed: 12': ''}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#当日成交\n",
    "user.today_trades"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12.45"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(\"12.45\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
